//
//  CycleView.m
//  BobNews
//
//  Created by lanou3g on 16/5/4.
//  Copyright © 2016年 YJC. All rights reserved.
//

#import "CycleView.h"

@interface CycleView ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *imgScrollView;
@property (nonatomic,strong) UIPageControl *imgPageControl;
//imageView的数组
@property (nonatomic,strong) NSMutableArray *imgViewArray;
//存放图片的数组
@property (nonatomic,strong) NSMutableArray *imgArray;
//当前展示图片的下标
@property (nonatomic,assign) NSInteger index;
//定时器
@property (nonatomic,strong) NSTimer *imgTimer;
//点击回调block
@property (nonatomic,copy)void(^tapBlock)(NSInteger index);
//间隔时间
@property (nonatomic,assign) NSTimeInterval interVal;

@end
@implementation CycleView
//初始化方法
- (instancetype)initWithFrame:(CGRect)frame interVal:(NSTimeInterval)interval {
    self = [super initWithFrame:frame];
    if (self) {
        self.imgViewArray = [NSMutableArray new];
        self.imgArray = [NSMutableArray new];
        //self.imgNameArray = [NSMutableArray new];
        self.imgScrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
        self.imgScrollView.contentSize = CGSizeMake(frame.size.width *3, frame.size.height);
        [self addSubview:self.imgScrollView];
        for (int i = 0; i < 3; i++) {
            UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(frame.size.width * i, 0, frame.size.width, frame.size.height)];
            [self.imgScrollView addSubview:imgView];
            [self.imgViewArray addObject:imgView];
        }
        self.imgScrollView.pagingEnabled = YES;
        self.imgScrollView.showsHorizontalScrollIndicator = NO;
        self.imgScrollView.delegate = self;
        self.interVal = interval;
        
    
        
    }
    return self;
}
//index的setter方法
- (void)setIndex:(NSInteger)index {
    if (_index != index) {
        _index = index;
        self.imgPageControl.currentPage = index;
    }
}
//图片滚动的方法
- (void)timerAction {
    [self.imgScrollView setContentOffset:CGPointMake(self.imgScrollView.frame.size.width * 2, 0) animated:YES];
    self.index = (self.index + 1)%self.imgNameArray.count;
}
//回位
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    [self.imgScrollView setContentOffset:CGPointMake(scrollView.frame.size.width, 0) animated:NO];
    [self layoutImages];
}
//imgNameArray的setter方法
- (void)setImgNameArray:(NSMutableArray *)imgNameArray {
    if (_imgNameArray != imgNameArray) {
        _imgNameArray = imgNameArray;
//        设置pageControl
        [self.imgPageControl removeFromSuperview];
        self.imgPageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, self.frame.size.height - 40, self.frame.size.width, 40)];
        [self addSubview:self.imgPageControl];
        self.imgPageControl.currentPage = 0;
        self.imgPageControl.numberOfPages = _imgNameArray.count;
        self.imgPageControl.enabled = NO;
        for (int i = 0; i < imgNameArray.count; i++) {
            UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgNameArray[i]]]];
            [self.imgArray addObject:image];
            if (i < 3) {
                [self.imgViewArray [i] setImage:image];
            }
        }
//        设置当前偏移量
        [self.imgScrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
        self.index = 0;
        [self layoutImages];
//        点击手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCycleViewAction:)];
        [self.imgViewArray[1] addGestureRecognizer:tap];
        [self.imgViewArray[1]  setUserInteractionEnabled:YES];
//        初始化timer
        if (self.interVal <= 0) {
            self.imgTimer = [NSTimer scheduledTimerWithTimeInterval:self.interVal target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        }else {
            self.imgTimer = [NSTimer scheduledTimerWithTimeInterval:self.interVal target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        }
    }
}
//手势事件
- (void)tapCycleViewAction:(UITapGestureRecognizer *)tap {
    if (self.tapBlock) {
        self.tapBlock(self.index);
    }
}
// 代理
//开始拖动
static CGFloat x = 0;
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    x = self.imgScrollView.contentOffset.x;
    [self.imgTimer setFireDate:[NSDate distantFuture]];
}
//减速结束
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (x > scrollView.contentOffset.x) {
        [self panToLeft:NO Index:self.index];
    }else if (x < scrollView.contentOffset.x) {
        [self panToLeft:YES Index:self.index];
    }
    [self.imgTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2]];
}

//向右滑动
- (void)panToLeft:(BOOL)left Index:(NSInteger)index {
    if (!left) {
        self.index = (index - 1 + self.imgNameArray.count) % self.imgNameArray.count;
    }else {
        self.index = (index + 1) % self.imgNameArray.count;
    }
    [self layoutImages];
    [self.imgScrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
}
- (void)layoutImages {
    for (int i = 0; i < 3; i++) {
        [self.imgViewArray[i] setImage:self.imgArray[(self.index - 1 + i + self.imgNameArray.count) % self.imgNameArray.count]];
    }
}

//添加回调
- (void)addTapBlock:(void (^)(NSInteger))block {
    self.tapBlock = block;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
